package com.weizhi.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weizhi.server.dto.RoleDto;
import com.weizhi.server.entity.Role;
import com.weizhi.server.mapper.RoleMapper;
import com.weizhi.server.service.RoleService;
import com.weizhi.server.util.PropertyCopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    private final static Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Resource
    private RoleMapper roleMapper;

    @Override
    public PageInfo list(JSONObject param) {
        PageHelper.startPage(param.getInteger("pageIndex"), param.getInteger("pageSize"));
        List<Role> roleList = roleMapper.list();
        PageInfo pageInfo = new PageInfo(roleList);
        List<RoleDto> roleDtoList = PropertyCopyUtil.copyList(roleList, RoleDto.class);
        pageInfo.setList(roleDtoList);
        return pageInfo;
    }

    @Override
    public List<RoleDto> all() {
        List<Role> roleList = roleMapper.list();
        List<RoleDto> roleDtoList = PropertyCopyUtil.copyList(roleList, RoleDto.class);



        return roleDtoList;
    }

    @Override
    public void save(RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        if(role.getId() == null){
            roleMapper.insertSelective(role);
        }else{
            roleMapper.updateByPrimaryKey(role);
        }

    }

    @Override
    @Transactional
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }
}
